package com.lobov.entities;

import java.io.Serializable;

import com.lobov.entities.yale.MatrixYale;
import com.lobov.entities.yale.SnipYale;
import com.lobov.entities.yale.SparseVector;

public class ContainerOfSnipsYale extends Timer implements Serializable {

	private static final long serialVersionUID = 1L;

	public ContainerOfSnipsYale(int numOfSnips, int snipsXLength) {
		this.snips = new SnipYale[numOfSnips];
		this.snipsXLength = snipsXLength;
	}

	private SnipYale[] snips = null;
	private String name;
	private int snipsXLength = 0;

	public SnipYale[] getSnips() {
		return snips;
	}

	public void setSnips(SnipYale[] snips) {
		this.snips = snips;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public SnipYale getSnipByIndex(int i) {
		return this.snips[i];
	}

	public void setSnipByIndex(SnipYale snip, int i) {
		this.snips[i] = snip;
	}

	public int getSnipsXLength() {
		return snipsXLength;
	}

	// получение из контейнера матрицы
	public MatrixYale toMatrix() throws Exception {
		// массив векторов будушей матрицы
		SparseVector[] vectors = new SparseVector[this.countVectors()];

		// порядковый номер вектора в матрице
		int sp_vector_num = 0;

		// перебор лоскутов контейнера
		for (SnipYale snipYale : snips) {
			// временный массив векторов из лоскута
			SparseVector[] tempVectors = new SparseVector[snipYale.getVectors().length];
			// перебор векторов в лоскуте
			for (int i = 0; i < snipYale.getVectors().length; i++) {
				vectors[sp_vector_num] = snipYale.getVectors()[i];
				sp_vector_num++;
			}
		}

		MatrixYale matrixYale = new MatrixYale(vectors, false);

		return matrixYale;
	}

	@Override
	public String toString() {

		try {
			return this.toMatrix().toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ERROR";
	}

	public int countVectors() {
		int num = 0;
		for (SnipYale snip : snips) {
			for (SparseVector vectot : snip.getVectors()){
				num++;
			}
		}
		return num;
	}

}
